/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controlenotas;

import java.math.BigDecimal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.JTableHeader;
import negocio.Aluno;
import negocio.IlegalNotaException;
import negocio.Nota;
import negocio.Turma;
import org.jdesktop.swingx.JXTable;

/**
 *
 * @author jeferson
 */
public class TurmaTableModel extends AbstractTableModel{

    //private ArrayList<Aluno> alunos;
    private Turma turma;
    private JXTable table;
    private JTableHeader header;
    private int offset = 1;

    public TurmaTableModel(Turma turma){
        this.turma = turma;
    }

    public Turma getTurma() {
        return turma;
    }

    public void setTurma(Turma turma) {
        this.turma = turma;
        fireTableDataChanged();
    }

    @Override
    public int getRowCount() {
        if(turma==null)
            return 0;
        //JOptionPane.showMessageDialog(null, turma.getAlunos().size());
        return turma.getAlunos().size();
    }

    @Override
    public int getColumnCount() {
        return 15;
    }

    @Override
    public Object getValueAt(int row, int column) {
        if(turma==null)
            return "";
        Aluno a = turma.getAluno(row+offset);
        BigDecimal v;
        while(a==null){
            offset++;
            a = turma.getAluno(row+offset);
        }
        switch (column) {
            case 0:
                return a.getNum();
            case 1:
                return a.getNome();
            case 2:
                return a.getNotaInString("N1");
            case 3:
                return a.getNotaInString("N2");
            case 4:
                return a.getNotaInString("N3");
            case 5:
                return a.getNotaInString("N4");
            case 6:
                return a.getNotaInString("M1");
            case 7:
                return a.getNotaInString("R1");
            case 8:
                return a.getNotaInString("M1S");
            case 9:
                return a.getNotaInString("M2");
            case 10:
                return a.getNotaInString("R2");
            case 11:
                return a.getNotaInString("M2S");
            case 12:
                return a.getNotaInString("MP");
            case 13:
                return a.getNotaInString("RF");
            case 14:
                return a.getNotaInString("MF");
        }
        return "";
    }

    @Override
    public String getColumnName(int column) {
        switch(column){
            case 0: return "Nº";
            case 1: return "ALUNO";
            case 2: return "N1";
            case 3: return "N2";
            case 4: return "N3";
            case 5: return "N4";
            case 6: return "M1SP";
            case 7: return "R1S";
            case 8: return "M1S";
            case 9: return "M2SP";
            case 10: return "R2S";
            case 11: return "M2S";
            case 12: return "MFP";
            case 13:return "RF";
            case 14: return "MF";
        }
        return super.getColumnName(column);
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        if(columnIndex==0)
            return Integer.class;
        return super.getColumnClass(columnIndex);
    }



    @Override
    public boolean isCellEditable(int row, int column) {
        if(column==1 || column==2 || column==3 || column==4 || column==5 || column==7 || column==10 || column==13)
            return true;
        return false;
    }

    @Override
    public void setValueAt(Object aValue, int row, int column) {
        if (turma == null) {
            return;
        }
        Aluno a = turma.getAluno(row + 1);
        String v = (String) aValue;
        Nota n = null;
        if (column > 1) {
            if (!v.equals("")) {
                v = v.replace(",", ".");
                try {
                    n = new Nota(Float.parseFloat(v));
                } catch (IlegalNotaException ex) {
                    Logger.getLogger(TurmaTableModel.class.getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(table, "Uma nota deve ter valor de 0 a 10");
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(table, "Uma nota deve ter valor de 0 a 10");
                }
            }
        }
        switch (column) {
            case 1:
                a.setNome(v);
                break;
            case 2:
                a.setNota("N1", n);
                break;
            case 3:
                a.setNota("N2", n);
                break;
            case 4:
                a.setNota("N3", n);
                break;
            case 5:
                a.setNota("N4", n);
                break;
            case 7:
                a.setNota("R1", n);
                break;
            case 10:
                a.setNota("R2", n);
                break;
            case 13:
                a.setNota("RF", n);
                break;
        }

    }
}
